package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IDeclarationTypeService;
import com.mdd.admin.validate.DeclarationTypeCreateValidate;
import com.mdd.admin.validate.DeclarationTypeUpdateValidate;
import com.mdd.admin.validate.DeclarationTypeSearchValidate;
import com.mdd.admin.vo.DeclarationTypeListedVo;
import com.mdd.admin.vo.DeclarationTypeDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.DeclarationType;
import com.mdd.common.mapper.DeclarationTypeMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 申报类型实现类
 * @author LikeAdmin
 */
@Service
public class DeclarationTypeServiceImpl implements IDeclarationTypeService {
        
    @Resource
    DeclarationTypeMapper declarationTypeMapper;

    /**
     * 申报类型列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<DeclarationTypeListedVo>
     */
    @Override
    public PageResult<DeclarationTypeListedVo> list(PageValidate pageValidate, DeclarationTypeSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<DeclarationType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        declarationTypeMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:typeName@type_name:str",
        });

        IPage<DeclarationType> iPage = declarationTypeMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<DeclarationTypeListedVo> list = new LinkedList<>();
        for(DeclarationType item : iPage.getRecords()) {
            DeclarationTypeListedVo vo = new DeclarationTypeListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 申报类型详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return DeclarationType
     */
    @Override
    public DeclarationTypeDetailVo detail(Integer id) {
        DeclarationType model = declarationTypeMapper.selectOne(
                new QueryWrapper<DeclarationType>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        DeclarationTypeDetailVo vo = new DeclarationTypeDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 申报类型新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(DeclarationTypeCreateValidate createValidate) {
        DeclarationType model = new DeclarationType();
        model.setTypeField(createValidate.getTypeField());
        model.setTypeName(createValidate.getTypeName());
        model.setCreator(createValidate.getCreator());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        declarationTypeMapper.insert(model);
    }

    /**
     * 申报类型编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(DeclarationTypeUpdateValidate updateValidate) {
        DeclarationType model = declarationTypeMapper.selectOne(
                new QueryWrapper<DeclarationType>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setTypeField(updateValidate.getTypeField());
        model.setTypeName(updateValidate.getTypeName());
        model.setModifier(updateValidate.getModifier());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        declarationTypeMapper.updateById(model);
    }

    /**
     * 申报类型删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        DeclarationType model = declarationTypeMapper.selectOne(
                new QueryWrapper<DeclarationType>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(System.currentTimeMillis() / 1000);
        declarationTypeMapper.updateById(model);
    }

}
